
import java.util.*;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.swing.JOptionPane;

/**
 *
 * @author BUDDHIMA
 */
public class GmailClient {
    private String userName;
    private String password;
    private String sendingHost;
    private int sendingPort;
    private String from;
    private String to;
    private String subject;
    private String text;
    private String receivingHost;
//    private int receivingPort;

    public void setAccountDetails(String userName, String password) {
        this.userName = userName;//sender's email can also use as User Name
        this.password = password;
    }

    public void sendGmail(String from, String to, String subject, String text) {
        // This will send mail from -->sender@gmail.com to -->receiver@gmail.com
        this.from = from;
        this.to = to;
        this.subject = subject;
        this.text = text;

        // For a Gmail account--sending mails-- host and port shold be as follows
        this.sendingHost = "smtp.gmail.com";
        this.sendingPort = 465;
        Properties props = new Properties();
        props.put("mail.smtp.host", this.sendingHost);
        props.put("mail.smtp.port", String.valueOf(this.sendingPort));
        props.put("mail.smtp.user", this.userName);
        props.put("mail.smtp.password", this.password);
        props.put("mail.smtp.auth", "true");
        Session session1 = Session.getDefaultInstance(props);
        Message simpleMessage = new MimeMessage(session1);
        //MIME stands for Multipurpose Internet Mail Extensions

        InternetAddress fromAddress = null;
        InternetAddress toAddress = null;

        try {
            fromAddress = new InternetAddress(this.from);
            toAddress = new InternetAddress(this.to);
        } catch (AddressException e) {
            String mes = "Sending email to: " + to + " failed !!!";
            JOptionPane.showMessageDialog(null, mes, "Falied to Send!!!",
                    JOptionPane.ERROR_MESSAGE);
        }

        try {
            simpleMessage.setFrom(fromAddress);
            simpleMessage.setRecipient(RecipientType.TO, toAddress);
            simpleMessage.setSubject(this.subject);
            simpleMessage.setText(this.text);
            Transport transport = session1.getTransport("smtps");
            transport.connect(sendingHost, sendingPort, this.userName, this.password);
            transport.sendMessage(simpleMessage, simpleMessage.getAllRecipients());
            transport.close();
            JOptionPane.showMessageDialog(
                    null, "Mail sent successfully ...", "Mail sent",
                    JOptionPane.PLAIN_MESSAGE);
        } catch (MessagingException e) {
            JOptionPane.showMessageDialog(
                    null, "Sending email to: " + to + " failed !!!",
                    "Falied to Send!!!", JOptionPane.ERROR_MESSAGE);
        }
    }

    public void readGmail() {
        this.receivingHost = "pop.gmail.com";
        Properties props2 = System.getProperties();
        props2.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props2.setProperty("mail.pop3.socketFactory.fallback", "false");
        props2.setProperty("mail.pop3.port", "995");
        props2.setProperty("mail.pop3.socketFactory.port", "995");
        Session session2 = Session.getDefaultInstance(props2, null);
        try {
            Store store = session2.getStore("imaps");
            store.connect(this.receivingHost, this.userName, this.password);
            Folder folder = store.getFolder("INBOX");
            folder.open(Folder.READ_ONLY);
            Message message[] = folder.getMessages();
            for (int i = 0; i < message.length; i++) {
                System.out.println(message[i].getSubject());
            }
            folder.close(true);
            store.close();
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }

    public static void main(String[] args) {
        String mailFrom = "nqquan89@gmail.com";
        String mailTo = "nqquan89@gmail.com";
        String senderPassword = "y3u#mngannam";
        String senderUserName = "nqquan89@gmail.com";
        String mailSubject = "Testing Mail";
        String mailText = "Have an Nice Day ...........!!!";
        GmailClient newGmailClient = new GmailClient();
        newGmailClient.setAccountDetails(senderUserName, senderPassword);
        //newGmailClient.sendGmail(mailFrom, mailTo, mailSubject, mailText);
        newGmailClient.readGmail();
    }
}